package com.xhb.ui.download;

import cn.hutool.core.collection.ConcurrentHashSet;
import com.teamdev.jxbrowser.download.Download;
import com.xhb.biz.config.APPConfig;
import com.xhb.biz.po.DownloadPO;
import com.xhb.ui.resources.Resources;
import com.xhb.ui.tools.UItools;
import lombok.Data;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 浏览器下面的下载栏
 */
@Data
public class BottomDownloadBar extends JPanel {

    private static BottomDownloadBar downloadBar = new BottomDownloadBar();

    private JPanel filesPanel;

    private Set<DownloadFilePanel> downloadFilePanelSet = new ConcurrentHashSet<>();

    private Map<String, DownloadFilePanel> idDownloadMap = new ConcurrentHashMap<>();

    private boolean visiable = true;

    public BottomDownloadBar() {
        setLayout(new BorderLayout());
        setBackground(Color.WHITE);
        filesPanel = createLeftFilesPanel();
        add(createRight(), BorderLayout.EAST);
        add(filesPanel, BorderLayout.CENTER);
    }


    /**
     * 右边的显示全部按钮和
     */
    public JPanel createRight() {
        JPanel right = new JPanel();
        right.setLayout(new FlowLayout(FlowLayout.RIGHT));
        right.setBackground(Color.WHITE);
        JButton showAll = new JButton();
        UItools.parseButton(showAll);
        showAll.setBackground(Color.WHITE);
        showAll.setText("显示全部");
        showAll.setSize(100, 50);
        showAll.addActionListener(event -> {
            try {
                Desktop.getDesktop().open(new File(APPConfig.downloadPath));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            //这里写显示全部代码
        });
        JButton close = new JButton();
        UItools.parseButton(close);
        close.setIcon(Resources.getIcon("close.png"));
        close.setRolloverIcon(Resources.getIcon("close.png"));
        close.setBackground(Color.WHITE);
        close.addActionListener(event -> {
            close();
        });
        right.add(showAll);
        right.add(close);
        return right;
    }

    public void clear() {
        for (DownloadFilePanel downloadFilePanel : downloadFilePanelSet) {
            downloadFilePanel.dispose();
        }
        downloadFilePanelSet.clear();
        idDownloadMap.clear();
        filesPanel.removeAll();
    }

    /**
     * 关闭
     */
    public void close() {
        setVisible(false);
        visiable = false;
        clear();
    }

    /**
     * 显示
     */
    public void open() {
        setVisible(false);
        setVisible(true);
        visiable = true;
    }

    /**
     * 左侧文件列表
     *
     * @return
     */
    public JPanel createLeftFilesPanel() {
        JPanel left = new JPanel();
        left.setLayout(new FlowLayout(FlowLayout.LEFT));
        left.setBackground(Color.WHITE);
        return left;
    }

    public void add(DownloadPO downloadPO) {
        this.visiable = true;
        DownloadFilePanel downloadFilePanel = new DownloadFilePanel(downloadPO);
        filesPanel.add(downloadFilePanel);
        add(filesPanel, BorderLayout.CENTER);
        downloadFilePanelSet.add(downloadFilePanel);
        idDownloadMap.put(downloadPO.getId(), downloadFilePanel);
    }

    public boolean getVisiable() {
        return visiable;
    }

    public static BottomDownloadBar getInstance() {
        return downloadBar;
    }

}
